/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：苏州新区青山路1号索迪实训基地
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 权力阳光数据管理平台_1.0
 * <p>
 * 作者: 110510(朱伟家)
 * <p>
 * <p>
 * 文件名: test.java
 * <p>
 * 类产生时间:2013-3-23 下午9:04:53
 * <p>
 * 负责人: 110510(朱伟家)
 * <p>
 * 所在组: 权力阳光数据管理平台
 * <p>
 * 所在部门: 开发一部-技术二部
 * <p>
 * email：zhuwj@oceansoft.com.cn
 * <p>
 * <p>
 */

package com.oceansoft.core.common.util.impexp4doc;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.heavenlake.wordapi.Document;


public class test {

	
	
	

	 
	public static void main(String[] args) {       
		java2word("test.doc");
	}
	
	
	private static void java2word(String modal)
	{
		String imgAFile = "h:\\aaa.jpg"; 
		 Document doc= null;
		 try {
		      doc = new Document();
		      String p = "h:\\"+modal;
		      System.out.println("mbpath =  "+p);
		      doc.open(p);
		      
		    //向表格里插入数据
		      List datalist = new ArrayList();
		      String[] array = new String[]{"住而我认为"};
		      datalist.add(array);
		      doc.replaceTable(1,1,2,datalist);
		      //doc.insert(new File(imgAFile));
		      String content = new String("朱伟家".getBytes("iso-8859-1"),"UTF-8");
		     // doc.insert("我爱啊");
		      doc.insertAtBookmark("name",content);
		      
		      
		      File f = new  File(imgAFile);
		      BufferedImage bi2=ImageIO.read( f);
		     // byte[] imgbyte = imageToBytes(bi2,"jpg");
		      byte[] imgbyte = getBytesFromFile(f);
		      String imgpath = "h:\\new.jpg";
		      File imgPath = getFileFromBytes(imgbyte,imgpath);
		      doc.insertAtBookmark("photo", imgPath);
		      imgPath.delete();
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    finally {
		      try {
		       
		    	  
		        doc.saveAs("h:\\ttt11.doc");
		        if(doc!=null)
	        	{
		        	doc.close(true);
		        	doc = null;
	        	}
		      }
		      catch (Exception e) {
		        e.printStackTrace();
		      }
		    }
	}
	public static byte[] getBytesFromFile(File f){
	     if (f == null){
	         return null;
	    }
	     try {
	         FileInputStream stream = new FileInputStream(f);
	         ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
	         byte[] b = new byte[1000];
	         int n;
	         while ((n = stream.read(b)) != -1)
	             out.write(b, 0, n);
	          stream.close();
	          out.close();
	          return out.toByteArray();
	      } catch (IOException e){
	     }
	      return null;
	   }
	 
	
	  public static File getFileFromBytes(byte[] b, String outputFile) {
	      BufferedOutputStream stream = null;
	       File file = null;
	       try {
	      file = new File(outputFile);
	           FileOutputStream fstream = new FileOutputStream(file);
	           stream = new BufferedOutputStream(fstream);
	           stream.write(b);
	       } catch (Exception e) {
	           e.printStackTrace();
	      } finally {
	          if (stream != null) {
	               try {
	                  stream.close();
	               } catch (IOException e1) {
	                  e1.printStackTrace();
	              }
	          }
	      }
	       return file;
	   }
	
		public static byte[] imageToBytes(BufferedImage image,String format){
		BufferedImage bImage=new BufferedImage(image.getWidth(null),image
		.getHeight(null),BufferedImage.TYPE_INT_ARGB);
		Graphics bg=bImage.getGraphics();
		bg.drawImage(image, 0,0,null);
		bg.dispose();
				
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		try{
		ImageIO.write(bImage,format,out);
		}catch(IOException e){
		e.printStackTrace();
		//Log.log(null,"imageToBytes():"e);
		}
		 return out.toByteArray();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
